const mongoose = require('mongoose')
const Schema = mongoose.Schema
const ObjectId = Schema.Types.ObjectId

const NoticeSchema = new Schema({
  // 通知类型（0为系统消息，1为好友申请，2为群组邀请，3为群组申请）
  type: {
    type: Number,
    default: 0
  },
  // 通知标题
  title: {
    type: String,
    default: ''
  },
  // 通知内容
  content: {
    type: String,
    default: ''
  },
  // 目标用户id
  targetUser: {
    type: ObjectId,
    ref: 'User'
  },
  // 目标群组id
  targetDialog: {
    type: ObjectId,
    ref: 'Dialog'
  },
  meta: {
    createdAt: {
      type: Date,
      default: Date.now()
    },
    updatedAt: {
      type: Date,
      default: Date.now()
    }
  },
  // 通知创建者
  createdBy: {
    type: ObjectId,
    ref: 'User'
  },
  // 通知接收人
  sendTo: {
    type: ObjectId,
    ref: 'User'
  }
})

NoticeSchema.pre('save', function(next) {
  if (this.isNew) {
    this.meta.createdAt = this.meta.updatedAt = Date.now()
  } else {
    this.meta.updatedAt = Date.now()
  }

  next()
})

mongoose.model('Notice', NoticeSchema)